//
//  newFeatureViewController.swift
//  weibo
//
//  Created by David on 15/11/25.
//  Copyright © 2015年 DV. All rights reserved.
//

import UIKit

class newFeatureViewController: UICollectionViewController {
    private  let DVCollectionCell = "DVCollectionCell"
    init(){
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.registerClass(newFeatureCell.self, forCellWithReuseIdentifier: DVCollectionCell)
        
        setupUI()
    }
    private func setupUI(){
        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(DVCollectionCell, forIndexPath: indexPath)as!newFeatureCell
        
//        cell.frame = view.bounds
        cell.imageIndex = indexPath.item
        cell.backgroundColor = (indexPath.row % 2==0) ? UIColor.redColor() : UIColor.grayColor()
        
        
        return cell
    }
    //显示好cell之后调用的方法
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last!
        if path.item == 3{
        let cell = collectionView.cellForItemAtIndexPath(path) as! newFeatureCell
            cell.showBTN()
        }
    }
}
   private class newFeatureCell : UICollectionViewCell{
    var button : UIButton = {
        let btn = UIButton()
        btn.setTitle("进入体验", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        return btn
    }()
    @objc private func btnClick(){
            NSNotificationCenter.defaultCenter().postNotificationName(DVSwitchMainInterfaceNotification, object: "main")
    }
    
    func showBTN(){
        button.hidden = false
        button.userInteractionEnabled = false
        button.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 8, options: [], animations: { () -> Void in
            self.button.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
         self.button.userInteractionEnabled = true
        }
    }
    var imageIndex = 0{
        didSet{
            imageView.image = UIImage(named: "new_feature_\(imageIndex+1)")
            button.hidden = true
        }
    }
    lazy var  imageView = UIImageView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(imageView)
        addSubview(button)
        button.sizeToFit()
        button.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        imageView.frame = bounds
        
        button.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -164))
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
